package com.meitun.mama.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

/**
 * Created by sunny01 on 2016/3/29.
 */
public class TestTextView extends TextView {

    private final String TAG = "TestTextView";
    private final boolean debug = true;

    public TestTextView(Context context) {
        super(context);
        init();
    }

    public TestTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TestTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void print(String content) {
        if (debug) Log.e(TAG, content);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
    private void init() {
        print("init data ################################# "+hashCode());
        addOnAttachStateChangeListener(new OnAttachStateChangeListener() {
            @Override
            public void onViewAttachedToWindow(final View v) {
                postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        print("onViewAttachedToWindow : "+v.hashCode() + " | " + getText());
                    }
                }, 1000);
            }

            @Override
            public void onViewDetachedFromWindow(View v) {
                print("onViewDetachedFromWindow : "+v.hashCode());
            }
        });
    }

    @Override
    protected void onVisibilityChanged(View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        print("onVisibilityChanged : " + hashCode() + " VISIBLE is " + (VISIBLE == visibility));
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        print("onAttachedToWindow : " + hashCode() + " | " + getText());
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        print("onDetachedFromWindow : " + hashCode());
    }

    @Override
    public void onStartTemporaryDetach() {
        super.onStartTemporaryDetach();
        print("onStartTemporaryDetach : " + hashCode());
    }

    @Override
    public void onFinishTemporaryDetach() {
        super.onFinishTemporaryDetach();
        print("onFinishTemporaryDetach : " + hashCode());
    }
}
